package com.example.pokemon_chat_room.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.fastjson.JSONObject;
import com.example.pokemon_chat_room.MainActivity;
import com.example.pokemon_chat_room.PokemonDetail;
import com.example.pokemon_chat_room.R;
import com.example.pokemon_chat_room.pojo.Pokemon;
import com.example.pokemon_chat_room.pokedex;
import com.example.pokemon_chat_room.util.MyImageView;

import java.util.List;

public class PokeDexAdapter extends RecyclerView.Adapter<PokeDexAdapter.ViewHolder> {

    private List<Pokemon> pokemonList;
    private View view;
    private Context context;

    public PokeDexAdapter(Context context, List<Pokemon> pokemonList) {
        this.context = context;
        this.pokemonList = pokemonList;
    }
    public static class ViewHolder extends RecyclerView.ViewHolder {
        private MyImageView pokemon_img;
        private TextView pokemon_txt;

        public ViewHolder(View view) {
            super(view);
            // Define click listener for the ViewHolder's View

            pokemon_img = view.findViewById(R.id.pokemon_img);
            pokemon_txt = view.findViewById(R.id.pokemon_txt);
        }

    }


    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_pokedex_item, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, @SuppressLint("RecyclerView") int position) {
        holder.pokemon_img.setImageURL("poke/cp" + pokemonList.get(position).getImage());
        holder.pokemon_txt.setText(pokemonList.get(position).getName());

        holder.pokemon_img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, PokemonDetail.class);
                intent.putExtra("pokemon", JSONObject.toJSON(pokemonList.get(position)).toString());
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return pokemonList.size();
    }



}
